package com.log.compone;

import lombok.extern.slf4j.Slf4j;
import org.junit.Test;

import java.io.*;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @Description TODO
 * @Author zhaoYi
 * @Date 2024/7/16 20:41
 */

@Slf4j
public class ChannelTest {

    @Test
    public void read() {
        try {
            FileInputStream fis = new FileInputStream("test.txt");
            FileChannel channel = fis.getChannel();
            ByteBuffer buffer = ByteBuffer.allocate(1024);
            channel.read(buffer);
            buffer.flip();
            System.out.println(new String(buffer.array(), 0, buffer.remaining()));
            channel.close();


        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Test
    public void write() {
        try {
            FileOutputStream fos = new FileOutputStream("test.txt");
            FileChannel channel = fos.getChannel();
            ByteBuffer buffer = ByteBuffer.allocate(1024);
            buffer.put("hello world".getBytes());
            buffer.flip();
            channel.write(buffer);
            channel.close();
            log.info("文件写入成功");

        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Test
    public void copy() throws Exception {
        FileInputStream fis = new FileInputStream("test.txt");
        FileOutputStream fos = new FileOutputStream("test2.txt");
        FileChannel isChannel = fis.getChannel();
        FileChannel osChannel = fos.getChannel();
        ByteBuffer buffer = ByteBuffer.allocate(1024);
        while (true){
            buffer.clear();
            int flag = isChannel.read(buffer);
            if (flag == -1) {
                break;
            }
            buffer.flip();
            osChannel.write(buffer);
        }
        isChannel.close();
        osChannel.close();
        System.out.println("文件复制成功");


    }

    @Test
    public void copy2() throws Exception {

        FileInputStream fis = new FileInputStream("test.txt");
        FileOutputStream fos = new FileOutputStream("test3.txt");
        FileChannel isChannel = fis.getChannel();
        FileChannel osChannel = fos.getChannel();
        ByteBuffer buffer = ByteBuffer.allocate(4);
        ByteBuffer buffer2 = ByteBuffer.allocate(1024);
        ByteBuffer[] buffers = {buffer, buffer2};
        isChannel.read(buffers);
        for (ByteBuffer byteBuffer : buffers) {
            byteBuffer.flip();
            System.out.println(new String(byteBuffer.array(),0,byteBuffer.arrayOffset()));
        }
        osChannel.write(buffers);
        isChannel.close();
        osChannel.close();
        System.out.println("文件复制成功");

    }

    @Test
    public void copy3() throws Exception {
        FileInputStream fis = new FileInputStream("test.txt");
        FileOutputStream fos = new FileOutputStream("test5.txt");
        FileChannel isChannel = fis.getChannel();
        FileChannel osChannel = fos.getChannel();
//        osChannel.transferFrom(isChannel,isChannel.position(), isChannel.size());
        isChannel.transferTo(isChannel.position(),isChannel.size(),osChannel);
        isChannel.close();
        osChannel.close();
        System.out.println("文件复制成功");

    }


}
